/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.main.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.common.utils.TraceUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.OperaPswConstant;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.FeeAppeal;
import com.tbit.main.pojo.RideLogDetailsVO;
import com.tbit.main.pojo.VO.FeeAppealVO;
import com.tbit.main.service.FeeAppealService;
import com.tbit.main.service.OperaPswService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 订单申诉
 *
 * @since 2024/6/18
 */
@RestController
@RequestMapping("/feeAppeal")
public class FeeAppealController {

    @Autowired
    private FeeAppealService feeAppealService;
    @Autowired
    private OperaPswService operaPswService;

    @RequestMapping("/getCount")
    public JsonResult getCount(FeeAppeal feeAppeal) {
        int count = feeAppealService.getcount( feeAppeal);
        return JsonResult.succ(count);
    }

    /**
     * 获取申诉列表
     *
     * @param feeAppealVO 请求参数
     * @return JsonResult
     */
    @RequestMapping("/getList")
    public JsonResult getList(FeeAppealVO feeAppealVO) {
        /**兼容旧版开始=========*/
        Integer accountId = feeAppealVO.getAccountId();
        if (accountId != null){
            List<FeeAppeal> feeAppealS = feeAppealService.getListOld(feeAppealVO);
            return JsonResult.succ(feeAppealS);
        }
        /**兼容旧版结束=========*/
        Assert.notNull(feeAppealVO.getPageNo(),"pageNo.notNull");
        Assert.notNull(feeAppealVO.getPageSize(),"pageSize.notNull");
        String accountIds = feeAppealVO.getAccountIds();
        String[] accountIdSplit = accountIds.split(",");
        feeAppealVO.setAccountIdList(feeAppealVO.convert(accountIdSplit));
        Page<FeeAppealVO> feeAppealPage= feeAppealService.getList(feeAppealVO);
        return JsonResult.succ((int) feeAppealPage.getTotal(),feeAppealPage.getRecords());
    }

    //获取详情
    @RequestMapping("/getDetail")
    public JsonResult getDetail(String orderNO,Integer accountId,@RequestParam(name = "mapType",defaultValue = "2",required = false) Integer mapType) {
        FeeAppeal feeAppeal= feeAppealService.getDetail(orderNO,accountId,mapType);
        return JsonResult.succ(feeAppeal);
    }

    /**
     * 订单费用申诉处理回复
     */
    @RequestMapping("/getresult")
    public JsonResult getresult(String token, FeeAppeal feeAppeal) {
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.DEPOSIT_OPERA_UPDATE_MONEY.equals(feeAppeal.getOperaPsw())) {
            operaPswService.checkOperaPsw(loginUser, OperaPswEnum.OPERA_PSW_ORDER_CONFIG.getModule(), feeAppeal.getOperaPsw());
        }


        String result = feeAppeal.getResult();
        if (result != null && result.length() > 235){
            return JsonResult.fail(0,"回复长度不能超过235个汉字",TraceUtil.getTraceIdByLocal());
        }
        Map<String, Object> detail = feeAppealService.getresult(token, feeAppeal);
        if(!(Boolean) detail.get("flag")){
            return JsonResult.fail(null,(String) detail.get("text"), TraceUtil.getTraceIdByLocal());
        }
        return JsonResult.succ(detail);

    }

    //操作前跳转界面
    @RequestMapping("/getByOrderId")
    public JsonResult getByOrderId(String orderNO) {
        RideLogDetailsVO rideLogDetailsVO = feeAppealService.getByOrderId(orderNO);
        return JsonResult.succ(rideLogDetailsVO);

    }

}
